<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/21
 * Time: 13:45
 */

namespace app\admin\model;


class GoldRoomsAgent extends DataBase
{
    protected $name = 'gold_rooms_agent';
    protected $autoWriteTimestamp = true;

    public function listData()
    {
        $page_config = array(
            'page' => input('offset', 1),
            'list_rows' => input('limit', 10),
        );

        $agent_type = input('agent_type',1);

        $data = $this
            ->alias('a')
            ->join('fw_gold_egg c','a.gold_egg_id  =c.id','left')
            ->field('a.*,c.egg_name')
            ->where('a.agent_type',$agent_type)
            ->paginate($page_config);

        foreach($data as $key => $value){
            if($value['gift_type'] == 1 || $value['gift_type'] == 2){
//                $giftInfo  = Gift::where(['id'=>$value['gift_id']])->field('gift_name,diamonds')->find();
                $giftInfo  = Gift::where(['id'=>$value['gift_id']])->field('gift_name')->find();
                $data[$key]['gift_name'] = $giftInfo['gift_name'];
//                $data[$key]['diamonds'] = $giftInfo['diamonds'];
                $data[$key]['diamonds'] = 0;
            }elseif($value['gift_type'] == 3){
                $giftInfo  = Prop::where(['id'=>$value['gift_id']])->field('prop_name as gift_name')->find();
                $data[$key]['gift_name'] = $giftInfo['gift_name'];
                $data[$key]['diamonds'] = '道具无价值';
            }
        }
        return $data;
    }

    public function agentIndexAdd()
    {
        $newData = input('post.');
        $map['gift_id']=$newData['gift_id'];
        $map['gold_egg_id']=$newData['gold_egg_id'];
        $map['gift_type']=$newData['gift_type'];
        $map['agent_type']=$newData['agent_type'];
        $res = $this->where($map)->find();
        if($res)  return ['code'=>-100,'msg'=>'该礼物已存在'];
        $re = $this->save($newData);
        if($re) return ['code'=>100,'msg'=>'添加成功'];
        else return ['code'=>-100,'msg'=>'添加失败'];
    }

    public function agentIndexEdit()
    {
        $newData = input('get.');
        $res = $this->save($newData,[ 'id' => $newData['id'] ]);
        dailyLog(['content'=>'修改了代理'.$newData['agent_type'].'库存ID:'.$newData['id'].'的默认库存为:'.$newData['default_stock'],'type'=>3]);
        if($res) return ['code'=>100,'msg'=>'修改成功'];
        else return ['code'=>-100,'msg'=>'修改失败'];
    }

    public function agentLoopGiftInfo()
    {
        $gift_type = input('gift_type');
        $gold_id = input('gold_id');
        $agent_type = input('agent_type');
        $stock_type = input('stock_type');
        if($stock_type ==1){
            $map[] = ['default_stock','=',0];
        }
        $map[] = ['agent_type','=',$agent_type];
        $map[] = ['gift_type','=',$gift_type];
        $map[] = ['gold_egg_id','=',$gold_id];
        // dump($map);
        $data = $this->field('id,gift_id')->where($map)->select()->toArray();
        if($gift_type == 1){
            foreach($data as $k=>$v){
                $giftInfo =  GiftUrl::where('id','=',$v['gift_id'])->field('gift_name,diamonds')->find();
                $data[$k]['name'] = $giftInfo['gift_name'];
            }
        }elseif ($gift_type == 3){
            foreach($data as $k=>$v){
                $giftInfo =  Prop::where('id','=',$v['gift_id'])->field('prop_name')->find();
                $data[$k]['name'] = $giftInfo['prop_name'];
            }
        }
        return ['code'=>100,'data'=>$data];
    }
}